import axios from 'axios'
import AppConfig from '../../config/app.config';
import md5 from 'md5';

const api = axios.create({
  baseURL: AppConfig.http_url,
  headers: {
    'X-PLATFORM': AppConfig.platform,
    'X-VERSION': AppConfig.version,
  }
})

api.interceptors.request.use( (config) => {
  const time = Date.now()
  let url;
  if (config.params) {
    let params = '';
    for (let key in config.params) {
      if (key) {
        params = params + `${key}=${config.params[key]}&`;
      }
    }
    url = `${config.url}${params.slice(0, params.length - 1)}`;
  } else {
    url = config.url;
  }


  config.headers['X-TS'] = time;
  config.headers['X-SIGN'] = md5(`aaa${url}${time}`)
  config.headers['X-UUID'] = AppConfig.uuid;
  config.headers['X-LANGUAGE'] = navigator.language;
  return config;
},(error) => {
  return Promise.reject(error);
})

// 添加响应拦截器
api.interceptors.response.use( (res) => {
  return res;
}, (error) => {
  return Promise.reject(error);
});

export default api
